﻿/*
 * Created by SharpDevelop.
 * User: mac
 * Date: 11.03.2007
 * Time: 12:52
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Text;
using TagTool.Core;

namespace TagTool
{
	
	/// <summary>
	/// Description of MetaDataDialog.
	/// </summary>
	public partial class MetaDataDialog
	{
		private INodeUIController nodeUI;
		
		public MetaDataDialog(INodeUIController nodeUI)
		{

			this.nodeUI = nodeUI;
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			String licenseDir = ProjectManager.Instance.AppDir + "\\license";
			String[] fi = System.IO.Directory.GetFiles(licenseDir);
			String[] split;
			
			foreach(string s in fi)
			{
				split = s.Split('\\');
				this.comboBoxLicense.Items.Add(split[split.Length - 1]);			
			}
			
			try
			{
				Stream stream = File.Open(ProjectManager.Instance.AppDir + "\\session.xml", FileMode.Open);
				StreamReader reader = new StreamReader(stream);
				Metadata = reader.ReadLine();
				stream.Close();
			}
			catch
			{
				SetDefault();
			}
		}
		
		
		void ButtonOKClick(object sender, System.EventArgs e)
		{
			Hide();
			File.WriteAllText(ProjectManager.Instance.AppDir + "\\session.xml", Metadata);			
		}
		
		
		public String Metadata
		{
			get 
			{
				StringBuilder builder = new StringBuilder();
				builder.Append("<Metadata>");
				
				builder.Append("	<SessionName>");
				builder.Append(textBoxSessionName.Text);
				builder.Append("	</SessionName>");
				
				builder.Append("	<Location>");
				builder.Append(textBoxLocation.Text);
				builder.Append("	</Location>");
				
				builder.Append("	<Artist>");
				builder.Append(textBoxArtist.Text);
				builder.Append("	</Artist>");
				
				builder.Append("	<License>");
				builder.Append(comboBoxLicense.Text);
				builder.Append("	</License>");
				
				builder.Append("	<LicenseText>");
				builder.Append(textBoxLicenseText.Text);
				builder.Append("	</LicenseText>");
				
				builder.Append("</Metadata>");
				
				return builder.ToString();
			}
			
			
			set
			{
				StringReader stream = new StringReader(value);
				XmlTextReader reader = new XmlTextReader(stream);
				String tag = "";
				while (reader.Read())
				{
					switch (reader.NodeType) 
					{
						case XmlNodeType.Document:
			            break;
			            case XmlNodeType.Element:
			            	tag = reader.Name;
			            	break;
			            case XmlNodeType.EndElement:
			            	break;
			            case XmlNodeType.Text:
			            	switch(tag)
				            {
			            		case "SessionName":
			            			textBoxSessionName.Text = reader.Value.Substring(0, reader.Value.Length - 1);
			            		break;
			            		case "Location":
			            			textBoxLocation.Text = reader.Value.Substring(0, reader.Value.Length - 1);
			            		break;
			            		case "Artist":
			            			textBoxArtist.Text = reader.Value.Substring(0, reader.Value.Length - 1);
			            		break;
			            		case "License":
			            			comboBoxLicense.Text = reader.Value.Substring(0, reader.Value.Length - 1);
			            		break;
			            		case "LicenseText":
			            			textBoxLicenseText.Text = reader.Value.Substring(0, reader.Value.Length - 1);
			            		break;
				            }
			            
			            break;
			        }
				}
			}
			
		}
		
		public String SessionName
		{
			get { return textBoxSessionName.Text; }
		}
		
		void MetaDataDialogLoad(object sender, System.EventArgs e)
		{
			
		}
		
		void ComboBoxLicenseSelectedIndexChanged(object sender, System.EventArgs e)
		{
			FileStream stream;
			stream = new FileStream(ProjectManager.Instance.AppDir + "\\license\\" + comboBoxLicense.Text, FileMode.Open);
			
			StreamReader reader = new StreamReader(stream);
			this.textBoxLicenseText.Text = reader.ReadToEnd();
			stream.Close();
		}

	}
}
